package com.fq.wechat.action;

import com.fq.wechat.constant.CommonConstant;
import com.fq.wechat.constant.MsgKey;
import com.fq.wechat.service.CC3200Service;
import com.fq.wechat.service.FollowerService;
import com.fq.wechat.util.RequestContainer;
import com.fq.wechat.util.ResponseContainer;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author jifang.
 * @since 2016/4/18 17:31.
 */
@Component
public class WXAction {

    @Autowired
    private CC3200Service service;

    @Autowired
    private FollowerService fService;

    /*台灯相关操作*/
    public void do_l_on(Map<String, String> data, HttpServletResponse response) {
        String msgKey = MsgKey.L_ON;
        String content;
        String url;
        String sequence = data.get(CommonConstant.SEQUENCE);
        if (service.getLight(sequence).equals(CommonConstant.ON)) {
            content = ResponseContainer.ERROR_TEXT.get(msgKey);
            url = ResponseContainer.ERROR_MUSIC.get(msgKey);
        } else {
            service.saveLight(CommonConstant.ON, sequence);
            content = ResponseContainer.TEXT.get(msgKey);
            url = ResponseContainer.MUSIC.get(msgKey);
        }
        sendResponse(data, content, url, response);
    }

    public void do_l_off(Map<String, String> data, HttpServletResponse response) {
        String msgKey = MsgKey.L_OFF;
        String content;
        String url;
        String sequence = data.get(CommonConstant.SEQUENCE);
        if (service.getLight(sequence).equals(CommonConstant.OFF)) {
            content = ResponseContainer.ERROR_TEXT.get(msgKey);
            url = ResponseContainer.ERROR_MUSIC.get(msgKey);
        } else {
            service.saveLight(CommonConstant.OFF, sequence);
            content = ResponseContainer.TEXT.get(msgKey);
            url = ResponseContainer.MUSIC.get(msgKey);
        }
        sendResponse(data, content, url, response);
    }

    public void do_l_status(Map<String, String> data, HttpServletResponse response) {
        String msgKey = MsgKey.L_STATUS;
        String sequence = data.get(CommonConstant.SEQUENCE);
        String dbStatus = service.getLight(sequence);
        String contentFormatter = ResponseContainer.TEXT.get(msgKey);
        String content = String.format(contentFormatter, dbStatus.equals(CommonConstant.OFF) ? "关闭" : "打开");
        String url = ResponseContainer.MUSIC.get(msgKey);

        sendResponse(data, content, url, response);
    }

    /*风扇相关操作*/
    public void do_f_on(Map<String, String> data, HttpServletResponse response) {
        String msgKey = MsgKey.F_ON;
        String content;
        String url;
        String sequence = data.get(CommonConstant.SEQUENCE);
        String fan = service.getFan(sequence);
        if (fan.equals(CommonConstant.ON)) {
            content = ResponseContainer.ERROR_TEXT.get(msgKey);
            url = ResponseContainer.ERROR_MUSIC.get(msgKey);
        } else {
            service.saveFan(CommonConstant.ON, sequence);
            content = ResponseContainer.TEXT.get(msgKey);
            url = ResponseContainer.MUSIC.get(msgKey);
        }
        sendResponse(data, content, url, response);
    }

    public void do_f_off(Map<String, String> data, HttpServletResponse response) {
        String msgKey = MsgKey.F_OFF;
        String content;
        String url;
        String sequence = data.get(CommonConstant.SEQUENCE);
        if (service.getFan(sequence).equals(CommonConstant.OFF)) {
            content = ResponseContainer.ERROR_TEXT.get(msgKey);
            url = ResponseContainer.ERROR_MUSIC.get(msgKey);
        } else {
            service.saveFan(CommonConstant.OFF, sequence);
            content = ResponseContainer.TEXT.get(msgKey);
            url = ResponseContainer.MUSIC.get(msgKey);
        }
        sendResponse(data, content, url, response);
    }

    public void do_f_faster(Map<String, String> data, HttpServletResponse response) {
        String msgKey = MsgKey.F_FASTER;
        String content;
        String url;
        // 拿到当前风扇状态 off/slow/on
        String sequence = data.get(CommonConstant.SEQUENCE);
        String curFanStat = service.getFan(sequence);
        if (curFanStat.equals(CommonConstant.FAST)) {
            content = ResponseContainer.ERROR_TEXT.get(msgKey);
            url = ResponseContainer.ERROR_MUSIC.get(msgKey);
        } else {
            // 加速
            int level = RequestContainer.FAN_LEVEL.get(curFanStat) + 1;
            // 拿到加速后的状态
            String upFanStat = RequestContainer.FAN_LEVEL.inverse().get(level);
            service.saveFan(upFanStat, sequence);
            content = ResponseContainer.TEXT.get(msgKey);
            url = ResponseContainer.MUSIC.get(msgKey);
        }
        sendResponse(data, content, url, response);
    }

    public void do_f_slower(Map<String, String> data, HttpServletResponse response) {
        String msgKey = MsgKey.F_SLOWER;
        String content;
        String url;
        // 拿到当前风扇状态 slow/on/fast
        String sequence = data.get(CommonConstant.SEQUENCE);
        String curFanStat = service.getFan(sequence);
        if (curFanStat.equals(CommonConstant.OFF)) {
            content = ResponseContainer.ERROR_TEXT.get(msgKey);
            url = ResponseContainer.ERROR_MUSIC.get(msgKey);
        } else {
            // 减速
            int level = RequestContainer.FAN_LEVEL.get(curFanStat) - 1;
            // 拿到减速后的状态
            String downFanStat = RequestContainer.FAN_LEVEL.inverse().get(level);
            service.saveFan(downFanStat, sequence);
            content = ResponseContainer.TEXT.get(msgKey);
            url = ResponseContainer.MUSIC.get(msgKey);
        }
        sendResponse(data, content, url, response);
    }

    public void do_f_status(Map<String, String> data, HttpServletResponse response) {
        String msgKey = MsgKey.F_STATUS;
        String sequence = data.get(CommonConstant.SEQUENCE);

        String dbStatus = service.getFan(sequence);
        String contentFormatter = ResponseContainer.TEXT.get(msgKey);
        String content = String.format(contentFormatter, ResponseContainer.FAN_STATUS.get(dbStatus));
        String url = ResponseContainer.MUSIC.get(msgKey);

        sendResponse(data, content, url, response);
    }

    /*温度相关操作*/
    public void do_t_status(Map<String, String> data, HttpServletResponse response) {
        String msgKey = MsgKey.T_STATUS;
        String sequence = data.get(CommonConstant.SEQUENCE);
        String temperature = service.getTemperature(sequence);
        String contentFormatter = ResponseContainer.TEXT.get(msgKey);
        String content = String.format(contentFormatter, temperature);
        String url = ResponseContainer.MUSIC.get(msgKey);

        sendResponse(data, content, url, response);
    }

    /*湿度相关操作*/
    public void do_h_status(Map<String, String> data, HttpServletResponse response) {
        String msgKey = MsgKey.H_STATUS;
        String sequence = data.get(CommonConstant.SEQUENCE);
        String humidity = service.getHumidity(sequence);
        String contentFormatter = ResponseContainer.TEXT.get(msgKey);
        String content = String.format(contentFormatter, humidity);
        String url = ResponseContainer.MUSIC.get(msgKey);

        sendResponse(data, content, url, response);
    }

    /*绑定设备*/
    public void do_d_binding(Map<String, String> data, HttpServletResponse response) {
        String content;
        String url = null;

        String bindingMsg = data.get(CommonConstant.CONTENT);
        if (Strings.isNullOrEmpty(bindingMsg)) {
            content = "绑定设备不成功, 请确定您是用文字操作, 谢谢";
        } else {
            int start = bindingMsg.indexOf("(");
            int end = bindingMsg.indexOf(")");
            if (start == -1 || end == -1) {
                content = "设备绑定不成功, 请确定您的设备ID使用了半角小括号()包起来了";
            } else {
                String follower = data.get(CommonConstant.FROM_USER_NAME);
                String sequence = bindingMsg.substring(start + 1, end);
                if (service.getSensor(sequence) != null) {
                    content = "该sequence已经被其他微信绑定, 请输入您自己的sequence序列号";
                } else {
                    // 首先将原来绑定的设备解除绑定
                    String originSequence = fService.getFollowerSequence(follower);
                    service.removeSensor(originSequence);

                    fService.addFollowerSequence(follower, sequence);
                    service.initSensor(sequence);
                    content = "设备(" + sequence + ")绑定成功";
                }
            }
        }

        sendResponse(data, content, url, response);
    }

    public void do_default(Map<String, String> data, HttpServletResponse response) {

        String msgKey = MsgKey.E_MANUAL;

        String reqMsg = data.get(CommonConstant.CONTENT);
        if (reqMsg == null) {
            reqMsg = data.get(CommonConstant.RECOGNITION);
        }
        if (reqMsg == null) {
            reqMsg = data.get(CommonConstant.EVENT_KEY);
        }
        if (reqMsg == null) {
            reqMsg = "";
        }

        String contentFormatter = ResponseContainer.TEXT.get(msgKey);
        String content = String.format(contentFormatter, reqMsg);
        String url = ResponseContainer.MUSIC.get(msgKey);
        sendResponse(data, content, url, response);
    }

    public void sendResponse(Map<String, String> requestData, String content, String url, HttpServletResponse response) {

        String formatter = ResponseContainer.VOICE_MSG_FORMATTER;
        String to = requestData.get(CommonConstant.FROM_USER_NAME);
        String from = requestData.get(CommonConstant.TO_USER_NAME);
        long createTime = System.currentTimeMillis();

        String respData = String.format(formatter, to, from, createTime, content, url, url);
        try {
            response.getWriter().print(respData);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
